package componentes;

import items.BetItem;
import items.BetSideItem;
import items.EntityItem;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import model.Bet;
import model.BetDecisionType;
import model.BetSide;
import model.BetStatus;
import model.Entity;
import model.Group;
import model.Member;
import model.Role;
import model.User;
import android.app.betmein.R;
import android.app.betmein.SApplication;
import android.framework.binding.BindableList;
import android.framework.binding.BindableValue;

public class CommunicationManager {
	
	private static CommunicationManager instance = new CommunicationManager();
	private CommunicationManager(){
		
	}
	public static CommunicationManager getInstance(){
		return instance;
	}
	
	private IServerApi proxy;
	
	/**
	 * List of bets that user should answer about them.
	 */
	private BindableList<BetItem> beforeJoinedBets;
	private BindableList<BetItem> participateBets;
	private BindableList<BetItem> judgmentBets;
	private BindableList<BetItem> watchListBets;
	
	public void userChooseBetSide(BetItem betItem, BetSideItem betSideItem){
		betItem.getBet().setChoosenBetSide(betSideItem.getBetSide().getValue());
		betItem.getBet().setRole(Role.PARTICIPANT);
		beforeJoinedBets.remove(betItem);
		participateBets.add(betItem);
	}
	
	public BindableList<BetItem> getParticipateBets(){
		if(participateBets == null){
			initBetsLists();
		}
		return participateBets;
	}
	
	public BindableList<BetItem> getJoinedRequestBets(){
		if(beforeJoinedBets == null){
			initBetsLists();
		}
		
		return beforeJoinedBets;
	}
	
	public BindableList<BetItem> getJudgmentBets(){
		if(judgmentBets == null){
			initBetsLists();
		}
		
		return judgmentBets;
	}
	
	public BindableList<BetItem> getWatchListBets(){
		if(watchListBets == null){
			initBetsLists();
		}
		return watchListBets;
	}
	
	private void initBetsLists(){
		Collection<Bet> result = createJoinedRequestBets();
		beforeJoinedBets = Mapper.getInstance().map(result, Mapper.getInstance().betConvertor);
		participateBets = new BindableList<BetItem>();
		judgmentBets = new BindableList<BetItem>();
		watchListBets = new BindableList<BetItem>();
	}
	
	private List<Bet> createJoinedRequestBets() {
		return Arrays.asList(createBet("Title1", "The Romainain"), createBet("Title2", "IDC"), createBet("Title3", "Keren-Or"));
	}
	
	private Bet createBet(String title, String entityName){
		Bet bet = new Bet();
		bet.getTitle().setText(title);
		bet.setCreateOnEntity(createGroup(entityName, "Desctiption"));
		bet.addBetSide(createBetSide("Side A"));
		bet.addBetSide(createBetSide("Side B"));
		bet.addBetSide(createBetSide("Side C"));
		bet.addBetSide(createBetSide("Side D"));
		bet.addBetSide(createBetSide("Side E"));
		return bet;
	}
	
	private BindableValue<BetSide> createBetSide(String description){
		BetSide result = new BetSide();
		result.getSideDescription().setText(description);
		return new BindableValue<BetSide>(result);
	}
	
	public BindableList<EntityItem> getEntities(UUID userId) {
		//UUID userID = UUID.randomUUID();
		//Entity[] entities = proxy.getEntities(userID);
		
		Entity[] entities = createEntities();
		
		EntityItem[] items = Mapper.getInstance().map(entities);
		
		BindableList<EntityItem> result = new BindableList<EntityItem>();
		for (EntityItem itemEntity : items) {
			result.add(itemEntity);
		}
		return result;
	}
	
	private Entity[] createEntities() {
		Entity[] result = {createGroup("Group 1", "Desc1"),
				createGroup("Group 2", "Desc2"),
				createGroup("Group 3", "Desc3"),
				createUser("Adam", "Desc1"),
				createUser("Shahar", "Desc2"),
				createUser("Dan", "Desc3")};
		return result;
	}
	
	private Entity createGroup(String name, String description){
		Group g = new Group();
		g.getEntityName().setText(name);
		g.getDescription().setText(description);
		g.setPicture(SApplication.getResourceManager().getDrawable(R.drawable.ic_launcher));
		g.getMembersList().add(createMember(createUser("Adam", "Desc1"), 100));
		g.getMembersList().add(createMember(createUser("Dan", "Desc2"), 50));
		g.getMembersList().add(createMember(createUser("Shahar", "Desc3"), 20));
		return g;
	}
	
	private User createUser(String name, String description){
		User u = new User(name, description);
		u.setPicture(SApplication.getResourceManager().getDrawable(R.drawable.ic_launcher));
		u.getMembersList().add(createMember(new User(name, "Desc1"), 100));
		u.getMembersList().add(createMember(new User("Moshe", "Desc2"), 50));
		return u;
	}
	
	private Member createMember(User user, long amount){
		return new Member(UUID.randomUUID(), user, amount);
	}
	public void userChooseNewSide(BetItem item, BetSideItem betSideItem) {
		item.getBet().setChoosenBetSide(betSideItem.getBetSide().getValue());
	}
	public void setUserAsJudge(BetItem item) {
		item.getBet().getDecisionType().setValue(BetDecisionType.JUDGE);
		item.getBet().setRole(Role.JUDGE);
		beforeJoinedBets.remove(item);
		judgmentBets.add(item);
	}
	public void userReject(BetItem item) {
		item.getBet().setRole(Role.VIEWER);
		beforeJoinedBets.remove(item);
		watchListBets.add(item);
	}
	
}
